package net.oschina.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.bean.News;
import net.oschina.app.bean.NewsList;
import net.oschina.app.util.PreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Ethan on 2017/4/1.
 */

public class NewsItemAdapter extends BaseRecyclerViewAdapter {
    private Context mContext;
    private List<News> list=new ArrayList<>();

    //构造函数
    public NewsItemAdapter(Context context,NewsList newsList) {
        super(context);
        this.mContext=context;
        this.list= newsList.getList();
    }

    //找到展示布局,并绑定Holder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.view_xrv_newsdetail_item, parent, false);
        NewsItemHolder holder = new NewsItemHolder(itemView);
        return holder;
    }

    //获取数据,并将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder,position);
        //获取数据,并将数据赋给控件
        NewsItemHolder newsItemHolder = (NewsItemHolder) holder;
        News news = list.get(position);
        int id = news.getId();
        //设置是否阅读
        String ids = PreferencesUtils.getString(MApp.mContext, "news_id", "");
        if (ids.contains(id+"")){
            newsItemHolder.mTvNewsdetailTitle.setTextColor(Color.parseColor("#838383"));
        }else{
            newsItemHolder.mTvNewsdetailTitle.setTextColor(Color.parseColor("#313031"));
        }
        newsItemHolder.mTvNewsdetailTitle.setText(news.getTitle());
//        if (position<2){
            Drawable today=mContext.getResources().getDrawable(R.drawable.widget_today_icon);
            today.setBounds(0,0,today.getMinimumWidth(),today.getMinimumHeight());
            newsItemHolder.mTvNewsdetailTitle.setCompoundDrawables(today,null,null,null);
            newsItemHolder.mTvNewsdetailTitle.setCompoundDrawablePadding(5);
//        }
        newsItemHolder.mTvNewsdetailDes.setText(news.getBody());
        newsItemHolder.mTvAuthor.setText(news.getAuthor());
        newsItemHolder.mTvPubDate.setText(news.getPubDate());
        newsItemHolder.mTvComment.setText(news.getCommentCount()+"");
    }

    //条目数量
    @Override
    public int getItemCount() {
        return list.size();
    }

    //创建ViewHolder类
    public class NewsItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_newsdetail_title)
        TextView mTvNewsdetailTitle;
        @BindView(R.id.tv_newsdetail_des)
        TextView mTvNewsdetailDes;
        @BindView(R.id.tv_author)
        TextView mTvAuthor;
        @BindView(R.id.tv_pub_date)
        TextView mTvPubDate;
        @BindView(R.id.tv_comment)
        TextView mTvComment;
        @BindView(R.id.ll_item_newsdetail)
        LinearLayout mLlItemNewsdetail;

        public NewsItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
